import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [{
    path: '/',
    name: 'Home',
    // component: () => import( '@/views/home/Home.vue'),
    redirect: "/home",
    meta: {
        title: "首页"
    }
}, {
    path: '/home',
    name: 'Article',
    component: () => import('@/views/home/Home.vue')
}, {
    path: '/movies',
    name: 'Movie',
    component: () => import('@/views/movie/Movie.vue')
}, {
    path: '/movies/mid/:mid',
    name: 'Movie',
    component: () => import('@/views/movie/MovieDetail.vue')
}, {
    path: '/moviesCN/mid/:mid',
    name: 'Movie',
    component: () => import('@/views/movie/CNMovieDetail.vue')
}, {
    path: '/search/genres/:genres',
    name: 'Search',
    component: () => import('@/views/movie/SearchDetail.vue')
}, {
    path: '/categories',
    name: 'Categories',
    component: () => import('@/views/tags/Categories.vue')
}, {
    path: '/archives',
    name: 'Archives',
    component: () => import('@/views/archives/Archives.vue')
}, {
    path: '/about',
    name: 'About',
    meta: {
        requireAuth: true,
        title: 'Change'
    },
    component: () => import('@/views/about/About.vue')
}, {
    path: '/404',
    name: '404',
    component: () => import('@/views/err/404.vue')
}]

const router = new VueRouter({
    mode: 'history',
    base: "/m/",
    routes,
    scrollBehavior(to, from, savedPosition) {
        return {
            x: 0,
            y: 0
        }
    }
})

export default router

router.afterEach((to, from, next) => {
    window.scrollTo(0, 0);
});
router.beforeEach((to, from, next) => {
    if (to.meta.requireAuth) { // 判断该路由是否需要登录权限
        if (localStorage.getItem("username") != null) {
            next()
        } else {
            next({
                path: '/404'
            })
        }
    } else {
        next();
    }
})